<?php
class ReflectorFormat
{
	function reflect($rawDoc)
	{
		jimport('joomla.html.pane');

		$document = JFactory::getDocument();
		$document->addStyleSheet('components/com_jclassdocumentor/assets/css/accordion.css');

		$pane =& JPane::getInstance('tabs');

		$wikiClassPrefix = 'Joomla!_Programmierung/Framework';


		$html = '';
		$html .=  $pane->startPane("the-pane");
		foreach ($rawDoc->classes as $class)
		{
		$wikiMethodsPages = array();
			$wikiClassPage = '';
			$wikiClassPage .= '{{RightTOC}}';
			$wikiClassPage .= "'''".$class->getName()."'''".' {{@todo|Beschreibung}}'.NL.NL;
			$since = '';
			$isStatic = false;
			if($class->getDocComment())
			{
				$wikiClassPage .= '<source lang="php">Der DocComment dient nur zur Referenz - bitte LÖSCHEN'.NL.$class->getDocComment().NL.'</source>'.NL.NL;
				$comment = explode(NL, $class->getDocComment());
				$searches = array('@since', '@static');
				foreach ($comment as $c)
				{
					foreach ($searches as $search)
					{
						if( strpos($c, $search))
						{
							if( $search == '@static'){ $isStatic = true; continue; }
							if( $search == '@since'){ $since = str_replace('@since', "'''@since'''", $c); }
						}
					}
				}//foreach
			}

			$parent = $class->getParentClass();

			$wikiClassPage .= '==Definiert in=='.NL;
			$wikiClassPage .= '{{folder|/libraries/joomla}}'.NL;

			$stars = 1;
			if( $class->subPackage )
			{
				$wikiClassPage .= str_repeat('*', $stars).'{{folder|'.$class->subPackage.'|red}}'.NL;
				$stars ++;
			}
			if( $class->subSubPackage )
			{
				$wikiClassPage .= str_repeat('*', $stars).'{{folder|'.$class->subSubPackage.'|green}}'.NL;
				$stars ++;
			}
			if( $class->subSubSubPackage )
			{
				$wikiClassPage .= str_repeat('*', $stars).'{{folder|'.$class->subSubSubPackage.'|yellow}}'.NL;
				$stars ++;
			}
			$wikiClassPage .= str_repeat('*', $stars).'{{file|'.$rawDoc->fileName.'}}'.NL.NL;

			$wikiClassPage .=($isStatic) ? "* '''@static'''".NL : '';
			$wikiClassPage .=($since) ? trim($since).NL : "* '''@since''' {{@todo}}".NL;

			$wikiClassPage .= '==Importieren=='.NL;
			$s = ($class->subSubPackage) ? $class->subSubPackage.'.' : '';
			$wikiClassPage .= '<source lang="php">jimport( \'joomla.'.$class->subPackage.'.'.$s.JFile::stripExt($rawDoc->fileName).'\' );</source>'.NL.NL;

			$wikiClassPage .= '==Erweitert=='.NL;
			$wikiClassPage .=( $parent ) ? '[['.$wikiClassPrefix.'/'.$parent->name.'|'.$parent->name.']]' : '* Keine';
			$wikiClassPage .= NL.NL;

			$wikiClassPage .= '==Erweitert von=='.NL;
			//@todo
			$extendedBy = '';
			$wikiClassPage .=( $extendedBy ) ? '[['.$wikiClassPrefix.'/'.$extendedBy->name.'|'.$extendedBy->name.']]' : '* Keine';
			$wikiClassPage .= NL.NL;

			$methods =  $class->getMethods();
			if($methods)
			{
				$wikiClassPage .= '==Methoden=='.NL;
				$wikiClassPage .= '{| class="wikitable"'.NL;
				$wikiClassPage .= '|-'.NL;
				$wikiClassPage .= '!Name'.NL;
				$wikiClassPage .= '!Beschreibung'.NL;
			}

			$displayClassName = '';
			foreach ($methods as $method)
			{
				$declaringClass= $method->getDeclaringClass()->getName();
				if( strtolower($declaringClass) != strtolower($class->getName()) )
				{
					continue;
				}

				$parameters = $method->getParameters();
				$wikiParams = array();
				$wikiParamsDesc = '';

				foreach( $parameters as $parameter )
				{

					if( $parameter->isDefaultValueAvailable())
					{
						$def = $parameter->getDefaultValue();

						if( $def === null)
						{
							$wikiDefault = 'null';
						}
						else if( $def === false )
						{
							$wikiDefault = 'false';
						}
						else if( $def === true )
						{
							$wikiDefault = 'true';
						}
						else if( $def === array() )
						{
							$wikiDefault = 'array()';
						}
						else if( $def === '' )
						{
							$wikiDefault = "''";
						}
						else
						{
							$wikiDefault = $def;
						}
					}
					else
					{
						$wikiDefault = 'NODEFAULT';
					}

					$wikiP = '$'.$parameter->getName();
					if( $parameter->isOptional() )
					{
						$wikiP = '['.$wikiP.']';
					}
					$wikiParams[] = $wikiP;

					$wikiParamsDesc .= '|-'.NL;
					$wikiParamsDesc .= '|<tt>'.$wikiP.'</tt>'.NL;
					$wikiParamsDesc .= '|<tt>{{@todo|Typ}}</tt>'.NL;
					$wikiParamsDesc .= '|{{@todo|Beschreibung}}'.NL;
					$wikiParamsDesc .=($wikiDefault === 'NODEFAULT') ? '| ---'.NL : '|<tt>'.$wikiDefault.'</tt>'.NL;// : '|<tt>{{@todo|Default}}</tt>'.NL;
				}//foreach parameters

				$comment = explode(NL, $method->getDocComment());
				$searches = array('@return', '@since', '@static');

				$syntaxAdds = '';
				$docComOptions = self::parseDocComment($method->getDocComment(), $searches);
				$syntaxAdds .=( $docComOptions->return ) ? $docComOptions->return.NL : "* '''@return''' {{mark|XXXX}} {{@todo}}".NL;
				$syntaxAdds .=( $docComOptions->since ) ? $docComOptions->since.NL : "* '''@since''' {{@todo}}".NL;

				$wikiMethodsPage = '';
				$wikiMethodsPage .= "{{RightTOC}}'''".$class->getName().'/'.$method->name."'''".' {{@todo|Beschreibung}}'.NL.NL;
				if($method->getDocComment())
				{
					$wikiMethodsPage .= '<source lang="php">Der DocComment dient nur zur Referenz - bitte entfernen'.NL.$method->getDocComment().'</source>'.NL.NL;
				}
				#					$wikiMethodsPage .= '==Definiert in=='.NL;
				#					$wikiMethodsPage .= '{{folder|/libraries/joomla}}'.NL;
				#					$wikiMethodsPage .= '*{{folder|'.$class->subPackage.'|red}}'.NL;
				#					if( $class->subSubPackage )
				#					{
				#						$wikiMethodsPage .= '**{{folder|'.$class->subSubPackage.'|red}}'.NL;
				#						$wikiMethodsPage .= '*';
				#					}
				#					$wikiMethodsPage .= '**{{file|'.$rawDoc->fileName.'}}'.NL.NL;
				$wikiMethodsPage .= '==Syntax=='.NL;
				$isStatic =($docComOptions->isStatic) ? true : false;//@todo
				$s =($isStatic) ? 'static ' : '';
				$wikiMethodsPage .= '<source lang="php">'.$s.$method->name.'( '.implode(', ', $wikiParams).' )</source>'.NL;
				$wikiMethodsPage .= $syntaxAdds.NL;

				if( $wikiParamsDesc )
				{
					$wikiMethodsPage .= '{| class="wikitable"'.NL;
					$wikiMethodsPage .= '!Parameter'.NL;
					$wikiMethodsPage .= '!Datentyp'.NL;
					$wikiMethodsPage .= '!Beschreibung'.NL;
					$wikiMethodsPage .= '!Standardwert'.NL;

					$wikiMethodsPage .= $wikiParamsDesc;
					$wikiMethodsPage .= '|}'.NL.NL;
				}

				$wikiMethodsPage .= '==Beispiel=='.NL;
				$wikiMethodsPage .= '{{@todo|Beispiele Baby...}}'.NL.NL;
				$wikiMethodsPage .= '<source lang="php">'.NL.'//--Dein Beispiel'.NL.'</source>'.NL.NL;

				$wikiMethodsPage .= '==Siehe auch=='.NL;
				$s =($class->subPackageName) ? $class->subPackageName.'/' : '';
				$wikiMethodsPage .= '* <tt>[http://api.joomla.org/Joomla-Framework/'.$s.$class->getName().'.html#'.$method->name.' '.$class->getName().'->'.$method->name.'()]</tt> auf api.joomla.org'.NL.NL;

				$wikiMethodsPage .= '==Source code=='.NL;

				$s = ($class->subPackage) ? $class->subPackage.'/' : '';
				$ss = ($class->subSubPackage) ? $class->subSubPackage.'/' : '';
				$wikiMethodsPage .= '<jcodedisplay>'.$class->getName().'/'.$method->name.'</jcodedisplay>'.NL.NL;

				$wikiMethodsPage .= '[[Kategorie:Experten|'.$method->name.']]'.NL;
				$wikiMethodsPage .= '[[Kategorie:Joomla! Programmierung|'.$method->name.']]'.NL;
				$wikiMethodsPage .= '[[Kategorie:Framework|'.$method->name.']]'.NL;
				$wikiMethodsPage .= '[[Kategorie:'.$class->getName().'|'.$method->name.']]'.NL;

				//--Everythig starting with a '_' will be ignored - aka private.. @todo change for 1.6
				if( $method->name == '_' || substr($method->name, 0, 1) != '_')
				{

					$wikiMethodsPages[$method->name] = $wikiMethodsPage;
					$wikiClassPage .= '|-'.NL;
					$wikiClassPage .= '| [['.$wikiClassPrefix.'/'.$class->getName().'/'.$method->name.'|'.$method->name.']]'.NL;
					$wikiClassPage .= '|{{@todo|beschreibung}}'.NL;
				}

		}//foreach methods

		if($methods)
		{
			$wikiClassPage .= '|}'.NL.NL;
		}
		$wikiClassPage .= '==Siehe auch=='.NL;
		$s =($class->subPackageName) ? $class->subPackageName.'/' : '';
		$wikiClassPage .= '* <tt>[http://api.joomla.org/Joomla-Framework/'.$s.$class->getName().'.html '.$class->getName().']</tt> auf api.joomla.org'.NL.NL;
		$wikiClassPage .= '[[Kategorie:Experten|'.$class->getName().']]'.NL;
		$wikiClassPage .= '[[Kategorie:Joomla! Programmierung|'.$class->getName().']]'.NL;
		$wikiClassPage .= '[[Kategorie:Framework|'.$class->getName().']]'.NL;
		$wikiClassPage .= '[[Kategorie:'.$class->getName().'|'.$class->getName().']]'.NL;

		$html .=  $pane->startPanel($class->getName().' ->', $class->getName().'-page');
		$html .= '<textarea  class="code" style="width: 97%" rows="30" id="'.$class->getName().'-xxpage" onfocus="$(\''.$class->getName().'-xxpage\').select();" onclick="$(\''.$class->getName().'-xxpage\').select();">'.htmlspecialchars($wikiClassPage).'</textarea>';
		$html .= $pane->endPanel();
		#$html .= '<h2>Methoden</h2>';

		foreach ($wikiMethodsPages as $pName => $page)
		{
			$title = $pName;
			$html .= $pane->startPanel($title, $title.'-page');
			#				$html .= '<h3>'.$class->getName().' -> '.$pName.'</h3>';
			#				$html .= '<pre>'.htmlspecialchars($page).'</pre>';
			$html .= '<textarea  class="code" style="width: 97%" rows="30" id="'.$title.'-xxpage" onfocus="$(\''.$title.'-xxpage\').select();" onclick="$(\''.$title.'-xxpage\').select();">'.htmlspecialchars($page).'</textarea>';
			$html .= $pane->endPanel();
		}//foreach

		}//foreach classes

		$html .= $pane->endPane();

		return $html;
	}//function

	/**
	 *
	 * @param $docComment string
	 * @param $searchFor array
	 * @return object
	 */
	private function parseDocComment($docComment, $searchFor, $enclosings=array())
	{
		if( ! count($enclosings))
		{

		}
		$DComm = new stdClass();
		$DComm->isStatic = false;
		$DComm->since = '';
		$DComm->return = '';
		$comment = explode(NL, $docComment);
		foreach ($comment as $c)
		{
			foreach ($searchFor as $search)
			{
				if( strpos($c, $search))
				{
					if( $search == '@static'){ $DComm->isStatic = true; continue; }
					if( $search == '@return')
					{
						//						preg_match('%return\w(w?)%', $c, $matches);
						//						print_r($matches);
						//						$s =

						$DComm->return = trim(str_replace('@return', "'''@return'''", $c));
					}
					if( $search == '@since'){ $DComm->since = trim(str_replace('@since', "'''@since'''", $c)); continue;}
					//					$c = str_replace($search, "'''".$search."'''", $c);
					//					$syntaxAdds .= trim($c).NL;
				}
			}
		}//foreach

		return $DComm;
	}//function

}//class
